package com.qubuyer.business.mine.viewmodel

import com.qubuyer.core.anno.AutoInject
import com.qubuyer.core.framework.BaseViewModel
import com.qubuyer.core.http.RetrofitCall
import com.qubuyer.repository.entitys.MessageEntity

class MsgViewModel : BaseViewModel() {

    var type = 1
    var page = 1

    @AutoInject
    private val model = MsgModel()

    fun loadMoreMsg(block: (success: Boolean, dataList: List<MessageEntity>?, page: Int, fromCache: Boolean) -> Unit) {
        page++
        RetrofitCall<List<MessageEntity>?>(lifecycleOwner, this) {
            api { model.loadMsgList(type, page) }
            success {
                block.invoke(true, it, page, false)
            }

            fail { list, _, _ ->
                block.invoke(false, list, page, false)
            }
        }
    }

    fun refreshMsg(block: (success: Boolean, dataList: List<MessageEntity>?, page: Int, fromCache: Boolean) -> Unit) {
        page = 1
        RetrofitCall<List<MessageEntity>?>(lifecycleOwner, this) {
            api { model.loadMsgList(type, page) }
            success {
                block.invoke(true, it, page, false)
            }

            fail { list, _, _ ->
                block.invoke(false, list, page, false)
            }
        }
    }
}